import { Component, OnInit } from '@angular/core';
import { UtilService } from '../../../core/util/util.service';
import { DispatchPlanService } from '../dispatch-plan.service';
import * as moment from 'moment';
import * as _ from 'lodash';
import { FetchService } from '../../../core/net/fetch.service';
import { Router, ActivatedRoute } from '@angular/router';

const monthOptions = [
    {value: "", label: "全部"},
    {value: "4", label: "4月"},
    {value: "5", label: "5月"},
    {value: "6", label: "6月"},
    {value: "7", label: "7月"},
    {value: "8", label: "8月"},
    {value: "9", label: "9月"},
    {value: "d", label: "冬灌"},
];

const mngOption = [
    {value: "1", label: "宁西公司跃进渠"},
    {value: "2", label: "西干渠管理处"},
    {value: "3", label: "红寺堡扬水管理处"},
    {value: "4", label: "秦汉渠管理处"},
    {value: "5", label: "盐环定扬水管理处"},
    {value: "6", label: "渠首管理处"},
    {value: "7", label: "汉延渠管理处"},
    {value: "8", label: "惠农渠管理处"},
    {value: "9", label: "固海扬水管理处"},
    {value: "10", label: "唐徕渠管理处"},
    {value: "11", label: "沙坡头水务局"},
    {value: "12", label: "七星渠管理处"},
];
@Component({
    selector: 'app-mng-report',
    templateUrl: './mng-report.component.html',
    styleUrls: ['./mng-report.component.scss']
})
export class MngReportComponent implements OnInit {
    monthOptions = monthOptions;
    mngOption = mngOption;
    listType = '2';   //  0-上报数据 1-指标查看 2-分解
    isDivBtnShow: boolean = false;  //  是否可分解
    year = "";    // 默认今年
    monthSelection = '';   //  4-9 d:冬灌
    currentManageId = JSON.parse(localStorage.userInfo).manageId;
    manageId: string = JSON.parse(localStorage.userInfo).manageId;
    monthPlanData = []; // 存储全部状态的数据
    monthPlanDataFilter = []; // 根据状态过滤数据

    constructor(
        private util: UtilService,
        private dispatchService: DispatchPlanService,
        private fetchService: FetchService,
        private router: Router,
        private routerInfo: ActivatedRoute,
    ) {
        this.listType = this.routerInfo.snapshot.queryParams['listType'] || '0';
        //  秦汉渠4、渠首6、沙坡头11（中卫市水务局） 可分解
        this.isDivBtnShow = ['4', '6', '11'].includes(this.manageId);
        if (this.manageId != '10001') {
            this.mngOption = mngOption.filter(item => {
                return item.value === this.manageId;
            });
        } else {
            this.manageId = this.mngOption[0].value;
        }
    }

    ngOnInit() {
        this.getMonthDetailData();
    }

    getListData(): void {
        // status   ： 0 未提交    1 已提交    2 已下发
        // listType ： 0 上报数据  1 指标查看   2 分解
        if (this.listType === '0') {
            this.monthPlanDataFilter = _.cloneDeep(this.monthPlanData);
        } else if (this.listType === '1') {
            this.monthPlanDataFilter = _.filter(this.monthPlanData, (item) => {
                return item.state === '2';
            });
        } else {
            this.monthPlanDataFilter = _.filter(this.monthPlanData, (item) => {
                return item.state === '2';
            });
        }
    }

    getMonthDetailData(): void {
        this.isDivBtnShow = ['4', '6', '11'].includes(this.manageId);
        const params = {
            reportId: 5,
            cycle: this.year ? moment(this.year).year() : "",
            m: this.monthSelection || "",
            manageId: this.manageId,
            orderByField: "",
            orderBy: "cycle desc,m  desc"
        };
        this.fetchService.post<any>('plan/wrsSchedulePlan/list', params).subscribe(res => {
            if (Array.isArray(res.list) && res.list.length) {
                this.monthPlanData = _.cloneDeep(res.list);
                this.getListData();
            } else {
                this.monthPlanData = [];
                this.monthPlanDataFilter = [];
            }
        });
    }

    onEdit(openType, data?, dataFlag?): void {
        let paramsData = {};
        let planName = "";
        if (data) {
            paramsData = {
                y: data.cycle ||  "",
                m: data.m || "",
                dataType : data.dataType || "",
                dataFlag: dataFlag || '',
                manageId: this.manageId,
            };
            planName = data.name;
            if (dataFlag === 2) {
                paramsData['action'] = 'divQuery';
            }
        }
        // this.router.navigate(['/dispatch-plan/mng-report-detail'] , {
        //     skipLocationChange: true,
        //     queryParams: {paramsData: JSON.stringify(paramsData), openType, planName, manageId: this.manageId, listType: this.listType}
        // });
    }

    division(data): void {

    }

    exportMonthFile(data, dataFlag): void {
        const params = {
            reportId: '5',
            y: data.cycle,
            m: data.m,
            dataType: data.dataType,
            manageId: this.manageId,
            dataFlag: dataFlag,
        };
        this.dispatchService.exportExcel(params, data.name, true);
    }

    reset(): void {
        this.year = "";    // 默认今年
        this.monthSelection = "";   //  4-9 d:冬灌
        const manageId = JSON.parse(localStorage.userInfo).manageId;
        this.getMonthDetailData();
    }
}
